package com.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Tuser;

import flexjson.JSONSerializer;

/**
 * Servlet implementation class JSONArrayServlet
 */
public class JSONArrayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JSONArrayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//准备ArrayList数据，用户列表
		List<Tuser> users = new ArrayList<Tuser>();
		Tuser user1 = new Tuser();
		user1.setUname("mc1");
		user1.setPwd("111");
		users.add(user1);
		Tuser user2 = new Tuser();
		user2.setUname("mc12");
		user2.setPwd("222");
		users.add(user2);
		Tuser user3 = new Tuser();
		user3.setUname("mc3");
		user3.setPwd("333");
		users.add(user3);
		
		//转化ArrayList对象为JSON数组字符串
		JSONSerializer json = new JSONSerializer();
		json.exclude("class");
		String jsonArray = json.serialize(users);
		response.getWriter().write(jsonArray);
	}

}
